﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;

namespace ROS.Application.DomainObjects
{
    internal class Money
    {
        #region Constants

        private static int[] cents = new int[] { 1, 10, 100, 1000 };

        #endregion

        #region Fields

        private long id;
        private decimal amount;
        private Currency currency;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the ID of this money object.
        /// </summary>
        public long ID
        {
            get { return id; }
            set { id = value; } 
        }

        /// <summary>
        /// Gets the amount of this Money.
        /// </summary>
        public decimal Amount
        {
            get { return amount / centFactor; }
        }

        /// <summary>
        /// Get the currency assigned to this Money.
        /// </summary>
        public Currency Currency
        {
            get { return currency; }
        }

        /// <summary>
        /// Get how many cents to round to.
        /// </summary>
        private int centFactor
        {
            get { return cents[currency.FractionDigits]; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new Money object.
        /// </summary>
        public Money(decimal amount, Currency currency)
        {
            this.id = 0;
            this.currency = currency;
            this.amount = Math.Round(amount * centFactor);
        }

        /// <summary>
        /// Construct a new Money object.
        /// </summary>
        public Money(long id, decimal amount, Currency currency)
        {
            this.id = id;
            this.currency = currency;
            this.amount = Math.Round(amount * centFactor);       
        }

        #endregion

        #region Methods

        /// <summary>
        /// Calculates the amount of money multiplied by some integer amount.
        /// </summary>
        /// <param name="money"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        public static Money operator *(Money money, int quantity)
        {
            return new Money(money.Amount * quantity, money.Currency);
        }
        public static Money operator *(int quantity, Money money)
        {
            return money * quantity;
        }

        public static Money operator +(Money moneyA, Money moneyB)
        {
            return new Money(moneyA.Amount + moneyB.Amount, moneyA.currency);
        }

        public override string ToString()
        {
            return Amount.ToString();
        }

        #endregion        
    }
}
